package com.ceit.controller;

import com.ceit.model.Account;
import com.ceit.service.AdministratorsS;

import com.ceit.service.loginS;
import com.ceit.util.JsonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * Created by 隼 on 2017/1/22.
 */
@Controller
public class LoginController {
    JsonMapper nonNullBinder = JsonMapper.nonEmptyMapper();
    @Autowired
    private loginS loginS;
    @Autowired
    private AdministratorsS ADMINS;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login() throws Exception {

        System.out.println("-------"+loginS.getRole(1));
        return "login";
    }

    @RequestMapping(value = "/loginServer",method = RequestMethod.POST)
    public void loginServer(HttpSession session,HttpServletRequest request, HttpServletResponse response, String name, String password) throws Exception {
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        System.out.println(name+password);
        int id=-1;
        //-1表示为空
         id=loginS.getID(name);
        String passwords=loginS.getPassord(id);
        System.out.println("id="+id+"password="+passwords);
        if(password==null||password==""||!password.equals(passwords)){
            StringBuffer b=new StringBuffer();
            b.append("<R>");
            b.append("<pass>NO</pass>");
            b.append("<www>login</www>");
            b.append("</R>");
            System.out.println(b.toString());
            out.println(b.toString());
        }else{
            int roleid=loginS.getRole(id);
            session.setAttribute("user", id);
            session.setAttribute("role", roleid);
            String www="5";
            switch (roleid){
                case 1:
                    www="administrators";
                    break;
                case 2:
                    www="two";
                    break;
                case 3:
                    www="three";
                    break;
                case 4:
                    www="four";
                    break;

            }
            StringBuffer b=new StringBuffer();
            b.append("<R>");
            b.append("<pass>Yes</pass>");
            b.append("<www>");
            b.append(www);
            b.append("</www>");
            b.append("</R>");
            System.out.println(b.toString());
            out.println(b.toString());

        }
        out.flush();
        out.close();



    }

//超级管理
    @RequestMapping(value = "/administrators",method = RequestMethod.GET)
    public  String administrators(HttpSession session) throws Exception{
        System.out.println(session.getAttribute("user")+"user");
        session.setAttribute("username", ADMINS.getadByuserID((Integer) session.getAttribute("user")).getAname());
        //获取超级管理名称
        return "administrators";
    }
    @RequestMapping(value = "/two",method = RequestMethod.GET)
    public  String two(){
        return "two";
    }
    @RequestMapping(value = "/three",method = RequestMethod.GET)
    public  String three(){
        return "three";
    }
    @RequestMapping(value = "/four",method = RequestMethod.GET)
    public  String four(){
        return "four";
    }

    @RequestMapping(value = "/success",method = RequestMethod.GET)
    public  String success(){
        return "success";
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public  String index(){
        return "index";
    }

    //退出登录
    @RequestMapping(value = "/loginout",method = RequestMethod.GET)
    public  String loginout(HttpSession session){
        // 清除session
        session.invalidate();
        return "redirect:/login";
    }







}
